package com.lewis.module.m2.api.bill.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.entity.controller.BaseController;
import com.lewis.module.m2.api.bill.code.entity.BillCode;
import com.lewis.module.m2.api.bill.code.service.IBillCodeService;
import com.lewis.module.m2.api.bill.code.vo.BillCodeDeleteVo;
import com.lewis.module.m2.api.sysUserQq.entity.SysUserQq;
import com.lewis.module.m2.api.sysUserQq.service.ISysUserQqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 账单编码Controller
 *
 * @author lewis
 * @since 2021-11-09
 */
@Slf4j
@Api(tags = "账单编码")
@RestController
@RequestMapping("/api/bill/code")
public class BillCodeApi extends BaseController {

	@Resource
	private IBillCodeService billCodeService;

	@Resource
	private ISysUserQqService sysUserQqService;

	@ApiOperation(value = "查询账单编码列表", notes = "")
	@GetMapping("/list")
	public BaseResult list(BillCode billCode) {
		List<String> msgList = new ArrayList<>();
		SysUserQq one = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, billCode.getQq())
		);
		if (one == null) {
			return BaseResult.fail().setMsg("不存在该用户");
		}
		List<BillCode> billCodes = billCodeService.list(
				new LambdaQueryWrapper<BillCode>()
						.eq(BillCode::getUserId, one.getUserId())
						.eq(BillCode::getIsDelete, 0)
		);
		if (billCodes.size() == 0) {
			return BaseResult.fail().setMsg("名下不存在账单");
		}

		for (BillCode code : billCodes) {
			msgList.add("【" + code.getId() + "】" + code.getBillName() + "\n");
		}
		return BaseResult.ok().setData(msgList);
	}

	@ApiOperation(value = "新增账单编码", notes = "")
	@PostMapping("/add")
	public BaseResult add(@RequestBody BillCode billCode) {
		List<String> msgList = new ArrayList<>();
		SysUserQq one = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, billCode.getQq())
		);
		if (one == null) {
			return BaseResult.fail().setMsg("不存在该用户");
		}
		billCode.setUserId(one.getUserId());
		if (billCodeService.save(billCode)) {
			msgList.add("创建完毕，要保持记账的好习惯哦~");
			return BaseResult.ok().setData(msgList);
		}

		return BaseResult.fail().setMsg("创建失败，请重试。");
	}

	@ApiOperation(value = "删除账单编码", notes = "")
	@PostMapping("/delete")
	public BaseResult delete(@RequestBody BillCodeDeleteVo billCodeDeleteVo) {

		List<String> msgList = new ArrayList<>();

		Integer billCode = billCodeDeleteVo.getBillCodeId();
		String qq = billCodeDeleteVo.getQq();

		SysUserQq one1 = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, qq)
		);
		if (one1 == null) {
			return BaseResult.fail().setMsg("不存在该用户");
		}

		BillCode one = billCodeService.getById(billCode);
		if (one == null) {
			return BaseResult.fail().setMsg("禁止删除他人账单");
		}
		one.setIsDelete(1);
		if (billCodeService.updateById(one)) {
			msgList.add("删除完毕，要保持记账的好习惯哦~");
			return BaseResult.ok().setData(msgList);
		}

		return BaseResult.fail().setMsg("删除失败，请重试。");
	}
}